package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.GuanzhuAdapter;
import com.jixiantubu.bean.UserBean;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Wo_guanzhuActivity extends Activity {
	ListView wo_guanzhu_lv;

	// 数据来源
	List<UserBean> list = new ArrayList<UserBean>();
	GuanzhuAdapter adapter;
	HttpUtils httpUtils;
	Intent intent;
	String username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_wo_guanzhu);
		intent = getIntent();
		username = intent.getStringExtra("username");
		initViews();
		initData();
	}

	private void initViews() {
		wo_guanzhu_lv = (ListView) findViewById(R.id.wo_guanzhu_lv);
		wo_guanzhu_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 取出当期行的id
				intent = new Intent(Wo_guanzhuActivity.this,
						GuanzhuderenActivity.class);
				intent.putExtra("user_id", list.get(position));
				intent.putExtra("username", list.get(position).getUser_name());
				startActivity(intent);
			}
		});
	}

	private void initData() {
		adapter = new GuanzhuAdapter(Wo_guanzhuActivity.this, list);
		wo_guanzhu_lv.setAdapter(adapter);
		// 向服务器发送请求，查询数据
		httpUtils = new HttpUtils();

		RequestParams requParams = new RequestParams();

		requParams.addBodyParameter("key", "1");
		requParams.addBodyParameter("username", username);
		Log.e("收藏的key和user_id", MyApplication.user_id);

		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/WoGuanzhu";
		httpUtils.send(HttpMethod.POST, url, requParams,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// 获取服务器端返回的数据结果
						String result = responseInfo.result;
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<UserBean>>() {
						}.getType();
						List<UserBean> list1 = gson.fromJson(result, typeOfT);
						list.addAll(list1);
						Log.e("hua------", list.toString());
						adapter.notifyDataSetChanged();

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub

					}
				});

	}

}
